﻿using System;
using System.Collections.Generic;
using System.Xml.Linq;
using Engine;

namespace Game
{
    public class TaskWidget : CanvasWidget
    {
        public ListPanelWidget m_kindsList;

        public ListPanelWidget m_itemsList;

        public ComponentTask m_componentTask;

        public BevelledRectangleWidget m_displaytaskWidget;

        public BevelledRectangleWidget m_displayrewardstaskWidget;

        public bool IsMove = false;

        public float m_displaytaskWidgetValue = 5f;

        public float m_displayrewardstaskWidgetValue = 10f;

        public TaskWidget(ComponentTask componentTask)
        {
            TaskWidget taskWidget = this;
            m_componentTask = componentTask;
            XElement node = ContentManager.Get<XElement>("Widgets/Task1/TaskWidget");
            LoadContents(this, node);
            m_kindsList = Children.Find<ListPanelWidget>("KindList");
            m_itemsList = Children.Find<ListPanelWidget>("TaskList");
            m_displaytaskWidget = Children.Find<BevelledRectangleWidget>("DisplayTask.BevelledRectangleWidget");
            m_displayrewardstaskWidget = Children.Find<BevelledRectangleWidget>("GetRewards.BevelledRectangleWidget");
            m_kindsList.ItemWidgetFactory = delegate (object item)
            {
                CanvasWidget canvasWidget = new CanvasWidget();
                LabelWidget labelWidget = new LabelWidget();
                BevelledRectangleWidget widget = new BevelledRectangleWidget
                {
                    BevelSize = -1f
                };
                labelWidget.Text = ModItemManger.GetName("TaskType." + (TaskType)item);
                labelWidget.HorizontalAlignment = WidgetAlignment.Center;
                labelWidget.VerticalAlignment = WidgetAlignment.Center;
                canvasWidget.Children.Add(widget);
                canvasWidget.Children.Add(labelWidget);
                return canvasWidget;
            };
            ListPanelWidget kindsList = m_kindsList;
            kindsList.ItemClicked = (Action<object>)Delegate.Combine(kindsList.ItemClicked, (Action<object>)delegate (object item)
            {
                TaskType taskType = (TaskType)item;
                taskWidget.m_itemsList.ClearItems();
                switch (taskType)
                {
                    case TaskType.Other:
                        {
                            foreach (KeyValuePair<string, Task> othertask in componentTask.m_othertasks)
                            {
                                taskWidget.m_itemsList.AddItem(othertask.Value);
                            }

                            break;
                        }
                    case TaskType.Main:
                        {
                            Log.Information("获取主任务执行");
                            taskWidget.m_itemsList.AddItem(ComponentTask.GetTask(componentTask.MainTaskIndex));
                            Log.Information($"移除任务已经执行,当前任务序号：{componentTask.MainTaskIndex}");
                            Log.Information("开始执行已完成任务添加");
                            for (int num = ComponentTask.m_alltasks.Count - 1; num >= 0; num--)
                            {
                                Log.Information("执行已完成任务添加1,");
                                Task task = ComponentTask.m_alltasks[num];
                                if (task.TaskType == TaskType.Main && task.Index < componentTask.MainTaskIndex)
                                {
                                    Log.Information($"执行已完成任务添加2,当前任务总数{ComponentTask.m_alltasks.Count}，当前序号{componentTask.MainTaskIndex}");
                                    task.Conditions = true;
                                    taskWidget.m_itemsList.AddItem(ComponentTask.GetTask(task.Index));
                                }
                            }

                            break;
                        }
                }
            });
            m_itemsList.ItemWidgetFactory = (object item) => new LabelWidget
            {
                Text = ((Task)item).Name
            };
            ListPanelWidget itemsList = m_itemsList;
            itemsList.ItemClicked = (Action<object>)Delegate.Combine(itemsList.ItemClicked, (Action<object>)delegate
            {
                taskWidget.IsMove = true;
            });
            m_kindsList.AddItem(TaskType.Main);
            m_kindsList.AddItem(TaskType.Other);
            m_itemsList.SelectionColor = ModItemManger.MainUiColor;
        }

        public override void Update()
        {
            if (m_itemsList.SelectedItem != null)
            {
                Task task = (Task)m_itemsList.SelectedItem;
                if (task.Conditions)
                {
                    Children.Find<LabelWidget>("DisplayTaskNameLabel").Text = task.Name + "（已完成）";
                }
                else
                {
                    Children.Find<LabelWidget>("DisplayTaskNameLabel").Text = task.Name;
                }

                Children.Find<LabelWidget>("DisplayTaskLabel").Text = task.Text;
                Children.Find<LabelWidget>("DisplayTaskRewardsLabel").Text = task.RewardsText;
                if (Children.Find<ButtonWidget>("RewardsButton").IsClicked)
                {
                    if (task.TaskType == TaskType.Main && m_componentTask.MainTaskConditions && task.Index == m_componentTask.MainTaskIndex)
                    {
                        task.Rewards(m_componentTask.Entity.FindComponent<ComponentPlayer>(), m_componentTask.Project.FindSubsystem<SubsystemWorldSystem>(), m_componentTask.Project.FindSubsystem<SubsystemPickables>());
                        m_componentTask.RemoveTask(task);
                        if (task.Index == 0)
                        {
                            string name = task.Name;
                            m_componentTask.Entity.FindComponent<ComponentPlayer>().ComponentGui.DisplayLargeMessage("这里究竟是哪里？不管了，我得先生存下去……", "任务：" + name + "已完成", 5f, 1f);
                        }
                        else if (task.Index == 1)
                        {
                            string name2 = task.Name;
                            m_componentTask.Entity.FindComponent<ComponentPlayer>().ComponentGui.DisplayLargeMessage("真是宏伟的血池！它到底是哪里来的？令人毛骨悚然", "任务：" + name2 + "已完成", 5f, 1f);
                        }
                        else if (task.Index == 2)
                        {
                            string name3 = task.Name;
                            m_componentTask.Entity.FindComponent<ComponentPlayer>().ComponentGui.DisplayLargeMessage("鸟人？可爱的女生？还是……", "任务：" + name3 + "已完成", 5f, 1f);
                        }
                        else if (task.Index == 3)
                        {
                            string name4 = task.Name;
                            m_componentTask.Entity.FindComponent<ComponentPlayer>().ComponentGui.DisplayLargeMessage("真是一个奇怪的人……", "任务：" + name4 + "已完成", 3f, 1f);
                        }
                        else if (task.Index == 4)
                        {
                            string name5 = task.Name;
                            m_componentTask.Entity.FindComponent<ComponentPlayer>().ComponentGui.DisplayLargeMessage("令人感慨……", "任务：" + name5 + "已完成", 3f, 1f);
                        }
                        else if (task.Index == 5)
                        {
                            string name6 = task.Name;
                            m_componentTask.Entity.FindComponent<ComponentPlayer>().ComponentGui.DisplayLargeMessage("安东尼博士，安息。", "任务：" + name6 + "已完成", 3f, 1f);
                        }
                    }
                    else
                    {
                        m_componentTask.Entity.FindComponent<ComponentGui>().DisplaySmallMessage(ModItemManger.GetName("TaskDisplay"), Color.White, blinking: true, playNotificationSound: true);
                    }
                }
            }
            else
            {
                IsMove = false;
            }

            if (IsMove)
            {
                if (m_displaytaskWidget.Size.X != 300f)
                {
                    m_displaytaskWidget.Size = new Vector2(m_displaytaskWidget.Size.X + m_displaytaskWidgetValue, 382f);
                }

                if (m_displaytaskWidget.Size.X == 300f && m_displayrewardstaskWidget.Size.X != 300f)
                {
                    m_displayrewardstaskWidget.Size = new Vector2(m_displayrewardstaskWidget.Size.X + m_displayrewardstaskWidgetValue, 382f);
                }

                if (m_displaytaskWidget.Size.X == 300f && m_displayrewardstaskWidget.Size.X == 300f)
                {
                    IsMove = false;
                    m_displayrewardstaskWidget.IsVisible = false;
                    m_displaytaskWidget.IsVisible = false;
                    Children.Find<CanvasWidget>("DisplayTask").IsVisible = true;
                    Children.Find<CanvasWidget>("GetRewards").IsVisible = true;
                }
            }
            else if (m_itemsList.SelectedItem == null)
            {
                m_displayrewardstaskWidget.IsVisible = true;
                m_displaytaskWidget.IsVisible = true;
                if (m_displaytaskWidget.Size.X != 0f && m_displayrewardstaskWidget.Size.X == 0f)
                {
                    m_displaytaskWidget.Size = new Vector2(m_displaytaskWidget.Size.X - m_displaytaskWidgetValue, 382f);
                }

                if (m_displayrewardstaskWidget.Size.X != 0f)
                {
                    m_displayrewardstaskWidget.Size = new Vector2(m_displayrewardstaskWidget.Size.X - m_displayrewardstaskWidgetValue, 382f);
                }

                Children.Find<CanvasWidget>("DisplayTask").IsVisible = false;
                Children.Find<CanvasWidget>("GetRewards").IsVisible = false;
            }
        }
    }
}